package com.vastcm.stuhealth.client.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.jidesoft.hssf.HssfTableFormat;
import com.jidesoft.hssf.HssfTableUtils;

public class JTableExportParam {
	private String reportTitle;
	private String tabTitle;
	private JTable exportTable;
	private List<MessageFormat[]> headerMsgFmtList;
	private List<MessageFormat[]> footerMsgFmtList;

	public static void exportToSheet(File saveFile, JTableExportParam param) throws IOException {
		OutputStream output = new FileOutputStream(saveFile);
		exportToSheet(output, param);
		output.close();
	}

	public static void exportToSheet(OutputStream outputStream, JTableExportParam param) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook();
		exportToSheet(workbook, param);
		workbook.write(outputStream);
	}

	public static void exportToSheet(File saveFile, JTableExportParam... params) throws IOException {
		OutputStream output = new FileOutputStream(saveFile);
		exportToSheet(output, params);
		output.close();
	}

	public static void exportToSheet(OutputStream outputStream, JTableExportParam... params) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook();
		for (JTableExportParam param : params)
			exportToSheet(workbook, param);
		workbook.write(outputStream);
	}

	private static void exportToSheet(HSSFWorkbook workbook, JTableExportParam param) throws IOException {
		HSSFSheet sheet = workbook.createSheet(param.getTabTitle());

		int startRow = 0;
		HSSFRow row;
		HSSFCell cell;
		{//构建标题
			// 生成一个样式
			HSSFCellStyle titleStyle = workbook.createCellStyle();
			// 设置这些样式
			//			titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
			//			titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			//			titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			//			titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			//			titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			//			titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			// 生成一个字体
			HSSFFont titleFont = workbook.createFont();
			//			titleFont.setColor(HSSFColor.VIOLET.index);
			titleFont.setFontHeightInPoints((short) 20);
			titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			// 把字体应用到当前的样式
			titleStyle.setFont(titleFont);
			//标题行
			row = sheet.createRow(startRow++);
			row.setHeight((short) (15 * 36));
			cell = row.createCell(0);
			cell.setCellStyle(titleStyle);
			cell.setCellValue(new HSSFRichTextString(param.getReportTitle()));
			sheet.addMergedRegion(new CellRangeAddress(startRow - 1, startRow - 1, 0, param.getExportTable().getColumnCount()));
			sheet.createRow(startRow++);//增加一行空行
		}

		// 生成一个样式
		HSSFCellStyle subTitleStyle = workbook.createCellStyle();
		// 设置这些样式
		//		subTitleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		//		subTitleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		//		subTitleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		//		subTitleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		//		subTitleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		//		subTitleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		subTitleStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 生成一个字体
		HSSFFont subTitleFont = workbook.createFont();
		//		subTitleFont.setColor(HSSFColor.VIOLET.index);
		subTitleFont.setFontHeightInPoints((short) 12);
		//		subTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		subTitleStyle.setFont(subTitleFont);
		int[] msgIndexArray;
		if (param.headerMsgFmtList != null) {//构建单据头的副标题行
			msgIndexArray = initMessageIndex(param);
			for (MessageFormat[] msgs : param.headerMsgFmtList) {
				row = sheet.createRow(startRow++);
				for (int i = 0; i < msgs.length; i++) {
					if (msgs[i] != null) {
						cell = row.createCell(msgIndexArray[i]);
						cell.setCellStyle(subTitleStyle);
						cell.setCellValue(new HSSFRichTextString(msgs[i].toPattern()));
					}
				}
			}
		}
		HssfTableFormat localHssfTableFormat = new HssfTableFormat();
		localHssfTableFormat.setFirstRow(0);
		localHssfTableFormat.setFirstColumn(0);
		localHssfTableFormat.setNumberOfRows(-1);
		localHssfTableFormat.setNumberOfColumns(-1);
		localHssfTableFormat.setStartRow(startRow);//导出的Excel，从这一行开始写入表格中的内容
		localHssfTableFormat.setStartColumn(0);
		localHssfTableFormat.setIncludeTableHeader(true);
		localHssfTableFormat.setCellValueConverter(null);
		localHssfTableFormat.setColumnNameConverter(null);
		HssfTableUtils.exportToSheet(param.getExportTable(), workbook, sheet, localHssfTableFormat);

		if (param.footerMsgFmtList != null) {//构建单据头的副标题行
			startRow = sheet.getLastRowNum();
			startRow++;
			sheet.createRow(startRow++);//增加一行空行
			msgIndexArray = initMessageIndex(param);
			for (MessageFormat[] msgs : param.footerMsgFmtList) {
				row = sheet.createRow(startRow++);
				for (int i = 0; i < msgs.length; i++) {
					if (msgs[i] != null) {
						cell = row.createCell(msgIndexArray[i]);
						cell.setCellStyle(subTitleStyle);
						cell.setCellValue(new HSSFRichTextString(msgs[i].toPattern()));
					}
				}
			}
		}
	}

	private static int[] initMessageIndex(JTableExportParam param) {
		JTable table = param.getExportTable();
		int columnCount = table.getColumnCount();
		int[] msgIndexArray = new int[3];
		if (columnCount == 1) {
			msgIndexArray[0] = 0;
			msgIndexArray[1] = 0;
			msgIndexArray[2] = 0;
		} else if (columnCount == 2) {
			msgIndexArray[0] = 0;
			msgIndexArray[1] = 1;
			msgIndexArray[2] = 1;
		} else if (columnCount == 3) {
			msgIndexArray[0] = 0;
			msgIndexArray[1] = 1;
			msgIndexArray[2] = 2;
		} else {
			int index = 0;
			while (table.getTableHeader().getColumnModel().getColumn(index).getWidth() == 0 && index < columnCount) {
				index++;
			}
			msgIndexArray[0] = index;
			index = columnCount / 2 - 1;
			while (table.getTableHeader().getColumnModel().getColumn(index).getWidth() == 0 && index < columnCount) {
				index++;
			}
			msgIndexArray[1] = index;

			index = columnCount > 8 ? columnCount - 2 : columnCount - 1;
			while (table.getTableHeader().getColumnModel().getColumn(index).getWidth() == 0 && index > 0) {
				index--;
			}
			msgIndexArray[2] = index;
		}
		return msgIndexArray;
	}

	public void addMessageFormat(boolean isheaderFormat, MessageFormat leftFormat, MessageFormat middleFormat, MessageFormat rightFormat) {
		if (isheaderFormat) {
			if (headerMsgFmtList == null) {
				headerMsgFmtList = new ArrayList<MessageFormat[]>();
			}
			headerMsgFmtList.add(new MessageFormat[] { leftFormat, middleFormat, rightFormat });
		} else {
			if (footerMsgFmtList == null) {
				footerMsgFmtList = new ArrayList<MessageFormat[]>();
			}
			footerMsgFmtList.add(new MessageFormat[] { leftFormat, middleFormat, rightFormat });
		}
	}

	public void addHeaderMessage(MessageFormat leftFormat, MessageFormat middleFormat, MessageFormat rightFormat) {
		addMessageFormat(true, leftFormat, middleFormat, rightFormat);
	}

	public void addFooterMessage(MessageFormat leftFormat, MessageFormat middleFormat, MessageFormat rightFormat) {
		addMessageFormat(false, leftFormat, middleFormat, rightFormat);
	}

	public String getReportTitle() {
		return reportTitle;
	}

	public void setReportTitle(String reportTitle) {
		this.reportTitle = reportTitle;
	}

	public String getTabTitle() {
		return tabTitle;
	}

	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	public JTable getExportTable() {
		return exportTable;
	}

	public void setExportTable(JTable exportTable) {
		this.exportTable = exportTable;
	}

	public List<MessageFormat[]> getHeaderMsgFmtList() {
		return headerMsgFmtList;
	}

	public List<MessageFormat[]> getFooterMsgFmtList() {
		return footerMsgFmtList;
	}
}